//
//  SLLoginViewController.m
//  MyChatDemo
//
//  Created by DragonYao on 2017/9/10.
//  Copyright © 2017年 DragonYao. All rights reserved.
//

#import "SLLoginViewController.h"
#import "SLRegisterController.h"
#import "SLBaseNativationController.h"

@interface SLLoginViewController ()<SLRegisterControllerDelegate>

@property (weak, nonatomic) IBOutlet UIView *loginBgView;
@property (weak, nonatomic) IBOutlet UITextField *userField;
@property (weak, nonatomic) IBOutlet UITextField *pwdField;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;
@property (weak, nonatomic) IBOutlet UIButton *forgetPwdBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightConstraint;

@end

@implementation SLLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
        self.leftConstraint.constant = 23.0;
        self.rightConstraint.constant = 23.0;
    }
    self.userField.text = [UserLocalInfo sharedUserInfo].userId;
    self.pwdField.placeholder = @"请输入登录密码";
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardEndEdit:)];
    [self.view addGestureRecognizer:tapGesture];
    
}

- (void)keyboardEndEdit:(UITapGestureRecognizer *)tap {
    [self.view endEditing:YES];
}

- (IBAction)loginBtn:(UIButton *)sender {
    [self.view endEditing:YES];
    if ([SLUtils isNilString:self.userField.text]) {
        [ProgressHUD showError:@"请输入用户名"]; return;
    }
    if ([SLUtils isNilString:self.pwdField.text]) {
        [ProgressHUD showError:@"请输入登录密码"]; return;
    }
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [XMPPManager sharedManager].registerOption = NO;
    __weak typeof(self) weakSelf = self;
    [[XMPPManager sharedManager] userLogin:^(XMPPResultType type) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
            switch (type) {
                case XMPPResultTypeLoginSuccess: {
                    //只有登录成功之后才会保存带沙河里面去
                    [UserLocalInfo sharedUserInfo].hasLogin = YES;
                    [UserLocalInfo sharedUserInfo].userId = weakSelf.userField.text;
                    [UserLocalInfo sharedUserInfo].userPwd = weakSelf.pwdField.text;
                    [weakSelf dismissViewControllerAnimated:NO completion:nil];
                    //切换到主控制器
                    UIStoryboard *mainStorebord = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                    weakSelf.view.window.rootViewController = [mainStorebord instantiateInitialViewController];
                }
                    break;
                case XMPPResultTypeLoginFailure:
                    [ProgressHUD showError:@"用户名或者密码错误！"];
                    break;
                case XMPPResultTypeNetError:
                    [ProgressHUD showError:@"网络出现问题！"];
                    break;
                default:
                    break;
            }
        });
    }];
}

- (IBAction)forgetPassword:(UIButton *)sender {
    
    
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    id registerNavi = segue.destinationViewController;
    if ([registerNavi isKindOfClass:[SLBaseNativationController class]]) {
        SLBaseNativationController *navi  = (SLBaseNativationController *)registerNavi;
        if ([navi.topViewController isKindOfClass:[SLRegisterController class]]) {
            SLRegisterController *registerVC = (SLRegisterController *)navi.topViewController;
            registerVC.delegate = self;
        }
    }
}

#pragma mark - SLRegisterControllerDelegate
- (void)registerHadFinished {
    SLog(@"注册完成");
    self.userField.text = [UserLocalInfo sharedUserInfo].userRegisterId;
    [ProgressHUD showSuccess:@"注册成功，请输入密码登录"];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
